#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMenuBar>
#include <QWheelEvent>
#include <QFileDialog>
#include <QDebug>
#include <QDir>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->resize(800, 500);
    ui->lastBtn->move(0, 140);
    ui->lastBtn->hide();
    ui->nextBtn->move(749, 140);
    ui->nextBtn->hide();
    this->setWindowTitle("图片查看器");
    this->setWindowIcon(QIcon(":/icons/icon"));
    ui->imageLabel->hide();
}


void MainWindow::wheelEvent(QWheelEvent *event)
{
    if(event->angleDelta().ry() == 120 && this->labelHeight < labelMaxSize && this->labelWeight < labelMaxSize)
    {
        this->labelHeight *= 1.2;
        this->labelWeight *= 1.2;
    }
    else if(event->angleDelta().ry() == -120 && this->labelHeight > labelMinSize && this->labelWeight > labelMinSize)
    {
        this->labelHeight *= 0.8;
        this->labelWeight *= 0.8;
    }

    ui->imageLabel->resize(this->labelWeight, this->labelHeight);
    ui->imageLabel->move((this->width() - this->labelWeight) / 2, (this->height() - this->labelHeight) / 2);
    ui->imageLabel->setPixmap(this->pix);
    ui->imageLabel->setScaledContents(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 根据打开图片的大小设置label大小并移动到窗口中央
void MainWindow::changePicSize()
{
    if(this->pix.width() > 500 || this->pix.height() > 500)
    {
        this->labelHeight = this->pix.height() / 5;
        this->labelWeight = this->pix.width() / 5;
    }
    else
    {
        this->labelHeight = this->pix.height();
        this->labelWeight = this->pix.width();
    }
    ui->imageLabel->resize(this->labelWeight, this->labelHeight);
    ui->imageLabel->move((this->width() - this->labelWeight) / 2, (this->height() - this->labelHeight) / 2);
}


void MainWindow::on_actionfile_triggered()
{
    QString filePath = QFileDialog::getOpenFileName(this, "打开文件", "e:/", "*.jpg;;*.png;;*.gif");
    if (filePath.isEmpty())
    {
        return;
    }
    QPixmap pix(filePath);
    this->pix = pix;
    this->changePicSize();
    ui->imageLabel->setFrameStyle(QFrame::Box);
    ui->imageLabel->setPixmap(pix);
    ui->imageLabel->setAlignment(Qt::AlignCenter);
    ui->imageLabel->setScaledContents(true);
    ui->imageLabel->show();
}

void MainWindow::on_actiondir_triggered()
{
    QString dirPath = QFileDialog::getExistingDirectory(this, "打开目录", "./");
    QDir dir(dirPath);
    dir.setFilter(QDir::Files);
    this->list = dir.entryInfoList({"*.jpg", "*.png", "*.gif"});
    this->pix = QPixmap(this->list[this->index].absoluteFilePath());
    this->changePicSize();
    ui->imageLabel->setPixmap(this->pix);
    ui->imageLabel->setScaledContents(true);
    ui->imageLabel->show();
    ui->lastBtn->show();
    ui->nextBtn->show();
}

void MainWindow::on_nextBtn_clicked()
{
    this->index++;
    if(this->index == this->list.size())
    {
        this->index = 0;
    }

    this->pix = QPixmap(this->list[this->index].absoluteFilePath());
    this->changePicSize();
    ui->imageLabel->setPixmap(this->pix);
}

void MainWindow::on_lastBtn_clicked()
{
    this->index--;
    if(this->index == -1)
    {
        this->index = this->list.size() - 1;
    }

    this->pix = QPixmap(this->list[this->index].absoluteFilePath());
    this->changePicSize();
    ui->imageLabel->setPixmap(this->pix);
}
